layui.use(['upload','element','table','form','laytpl'],function(){
			 var $ = layui.jquery
  		,upload = layui.upload
			,table = layui.table
			,layer = layui.layer
			,element =layui.element
			,form = layui.form
			,laytpl=layui.laytpl
		//添加试卷 单个
		
		//取sessionStorage里面的userKey
		var teacherInfo = document.getElementById("teacherInfo").innerHTML;		
		var userKey = JSON.parse(sessionStorage.getItem("userKey"));
		console.log(userKey)
		if(userKey) {
			//如果存在,则说明登录过,先比对过期时间
			var delayTime = userKey.delayTime
			var nowTime = new Date().getTime()
			if(nowTime < delayTime) {
				//登录未失效
				laytpl(teacherInfo).render(userKey, function(html) {
					$("#teacherInfoView").html(html)
					element.render()
					
				})
			}
		}
		
		$("#submitPaper").click(function(){
			var title = $("#title").val(),
			author = $("#author").val()
			  $.ajax({
					type:'post',
					url:'http://localhost:8888/paper/addOrUpdate',
					data:{"title":title,"author":author},
					success:function(data){
						
						layer.msg("添加成功")
						
					},
					error:function(){
					
						
					}
				});  
		})
		
		
		
		var paperList = table.render({
					elem:'#paperList',
					url:"http://localhost:8888/paper/getPaperList",
					cols:[[
						  {field:'id', width:80, title: 'ID', sort: true}
				      ,{field:'title',  title: '标题'}
				      ,{field:'author',width:80,  title: '作者', sort: true}
				      ,{field:'create_at',  title: '创建时间', sort: true}
				      ,{fixed:'right',align:'center',title:"操作",toolbar:'#bar'}
					]]
			});
			
			table.on('tool(paperList)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
				  var data = obj.data; //获得当前行数据
				  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
				  var tr = obj.tr; //获得当前行 tr 的DOM对象
				 
				  if(layEvent === 'detail'){ //查看
				    //do somehing
				  } else if(layEvent === 'del'){ //删除
				    layer.confirm('真的删除行么', function(index){
				      obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
//				      console.log(data.id)
				      layer.close(index);
//				      向服务端发送删除指令
				      $.ajax({
				      	url:"http://localhost:8888/paper/deleteById?id="+data.id,
				      	type:"get",
				      	success:function(data){
				      		layer.msg("删除成功")
				      	}
				      })
				    });
				  } else if(layEvent === 'edit'){ //编辑
				    //do something
				   
				    
				    
				    //iframe窗
				 	layer.open({
					      type: 2,
					      title: obj.data.title,
					      shadeClose: true,
					      shade: true,
					      maxmin: true, //开启最大化最小化按钮
					      area: ['800px', '640px'],
					      content: "editPaper.html?paperId="+data.id,
					      cancel: function(index, layero){ 
						  	paperList.reload()
						    layer.close(index)							
						}    
			    	});
				 
				    
				  }
			});
	})